# -*- coding: utf-8 -*-
import sys
from dysms_python.aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from dysms_python.aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
from aliyunsdkcore.client import AcsClient
import uuid
from aliyunsdkcore.profile import region_provider
import random

"""
短信业务调用接口示例，版本号：v20170525

Created on 2017-06-12

"""
try:
    reload(sys)
    sys.setdefaultencoding('utf8')
except NameError:
    pass
except Exception as err:
    raise err

# 注意：不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
ACCESS_KEY_ID = "LTAIBniSJXLivWFf"
ACCESS_KEY_SECRET = "R6J51dkmbHorYsMbr3B8hATKpRqqjc"

acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
# region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)

'''
生成六位短信验证码
'''
def getRandomInt(num):
    temp=''
    for i in range(num):
        temp=temp+chr(random.randint(48,57))
    return temp

def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
    smsRequest = SendSmsRequest.SendSmsRequest()
    # 申请的短信模板编码,必填
    smsRequest.set_TemplateCode(template_code)

    # 短信模板变量参数
    if template_param is not None:
        smsRequest.set_TemplateParam(template_param)

    # 设置业务请求流水号，必填。
    smsRequest.set_OutId(business_id)

    # 短信签名
    smsRequest.set_SignName(sign_name)
    smsRequest.set_PhoneNumbers(phone_numbers)

    # 调用短信发送接口，返回json
    smsResponse = acs_client.do_action_with_exception(smsRequest)

    # TODO 业务处理

    return smsResponse

def sendSMS(tel):
    code=getRandomInt(6)
    __business_id = uuid.uuid1()
    send_sms(__business_id, tel, "中畜智联", "SMS_151177330", {"smsCode":code})
    return code


if __name__ == '__main__':
    # __business_id = uuid.uuid1()
    # # print(__business_id)
    # params = "{\"code\":\"12345\",\"product\":\"云通信\"}"
    # # params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}'
    # print(send_sms(__business_id, "13203108051", "云通信测试", "SMS_5250008", params))
    sendSMS('15342344775')




